/*
 * @version: 1.0
 * @Author: zhl
 * @Date: 2021-09-10 13:45:25
 * @LastEditors: zhl
 * @LastEditTime: 2021-09-26 13:28:21
 * @Description: 文件实现描述
 */

#include "Client-APDU.h"
#include "GET-Request.h"
#include "SET-Request.h"
#include "CONNECT-Request.h"
#include "RELEASE-Request.h"
#include "ACTION-Request.h"
#include "REPORT-Response.h"
#include "PROXY-Request.h"
#include "ERROR-Response.h"
#include "CLogger.h"

#define CLIENT_APDU_LEN 1000


Client_APDU Client_APDU::c_client_apdu; 


Client_APDU::Client_APDU(/* args */){
    c_apdu.apdu_len = 0;
    c_apdu.apdu_tag = 0;
    // c_apdu.apdu = (uint8_t *)malloc(c_apdu.apdu_max_len);
}

Client_APDU::~Client_APDU()
{

}

Client_APDU& Client_APDU::getInstance(){
    return c_client_apdu;
}

APDU& Client_APDU::packet(CLIENT_APDU_TAG tag){
    c_apdu.apdu_len = 0;
    c_apdu.apdu_tag = tag;
    c_apdu.apdu[0] = tag;
    c_apdu.apdu_len++;
    switch(tag){
        case CLIENT_CONNECT_REQUEST:
        break;
        case CLIENT_RELEASE_REQUEST:
        break;
        case CLIENT_GET_REQUEST:
        break;    
        case CLIENT_SET_REQUEST:
        break; 
        case CLIENT_ACTION_REQUEST:
        break;
        case CLIENT_REPORT_RESPONSE:
        break;
        case CLIENT_PROXY_REQUEST:
        break; 
        case CLIENT_ERROR_RESPONSE:
        break;
        default:
        break;
    }
}
uint8_t Client_APDU::get_apdu_resp_tag(uint8_t tag){
    uint8_t apdu_tag = tag;
    switch (tag){
        case CLIENT_CONNECT_REQUEST:
            apdu_tag = SERVER_CONNECT_RESPONSE;
            break;
        case CLIENT_RELEASE_REQUEST:
            apdu_tag = SERVER_RELEASE_RESPONSE;
            break;
        case CLIENT_GET_REQUEST:
            apdu_tag = SERVER_GET_RESPONSE;
            break;
        case CLIENT_SET_REQUEST:
            apdu_tag = SERVER_SET_RESPONSE;
            break;
        case CLIENT_ACTION_REQUEST:
            apdu_tag = SERVER_ACTION_RESPONSE;
            break;
        case CLIENT_REPORT_RESPONSE:
            apdu_tag = SERVER_REPORT_NOTIFICATION;
            break;
        case CLIENT_PROXY_REQUEST:
            apdu_tag = SERVER_PROXY_RESPONSE;
            break;
        case CLIENT_ERROR_RESPONSE:
            apdu_tag = SERVER_ERROR_RESPONSE;
            break;
        default:
            break;
    }
    return apdu_tag;
}

int Client_APDU::handle(DLT698_FRAME& frame, DLT698_BUFFER& data){
    data.len = 1;
    data.data[0] = get_apdu_resp_tag(frame.apdu.apdu_tag);
    printf("apdu_tag =%02X   resp_apdu_tag = %02X\n", frame.apdu.apdu_tag, data.data[0]);
    switch (frame.apdu.apdu_tag){
        case CLIENT_CONNECT_REQUEST:
            /* code */
            break;
        case CLIENT_RELEASE_REQUEST:
            break;
        case CLIENT_GET_REQUEST:
            GETRequest::getInstance().handle(frame, ConnectRequest::getInstance().get_connect_req_params(), data);
            break;
        case CLIENT_SET_REQUEST:
            break;
        case CLIENT_ACTION_REQUEST:
            break;
        case CLIENT_REPORT_RESPONSE:
            break;
        case CLIENT_PROXY_REQUEST:
            break;
        case CLIENT_ERROR_RESPONSE:
            break;
        default:
            break;
    }
    return 0;     
}